﻿using System;
using System.Linq;

namespace CloudMusic.Helper
{
    public class DateTimeHelper
    {
        /// <summary> 
        /// 获取时间戳 
        /// </summary> 
        /// <returns>UTC</returns> 
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 转换时间戳为C#时间
        /// </summary>
        /// <param name="timeStamp">时间戳 单位：毫秒</param>
        /// <returns>C#时间</returns>
        public static DateTime ConvertTimeStampToDateTime(long timeStamp)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            DateTime dt = startTime.AddMilliseconds(timeStamp);
            return dt;
        }
        /// <summary>
        /// 获取当天号数
        /// </summary>
        /// <returns></returns>
        public static string GetDays()
        {
            string date = DateTime.Now.ToString("dd");
            if (Convert.ToInt16(date) < 10)
            {
                date = date.Substring(1);
            }
            return date;
        }
        /// <summary>
        /// 获取当天月数号数
        /// </summary>
        /// <returns></returns>
        public static string GetMDays()
        {
            string date = DateTime.Now.ToString("MM月dd日");
            return date;
        }
        private string GetNYR(string y)
        {
            if (y.Length > 0)
            {
                string[] Y = y.Split('/');
                string _y = Y[0] + "年";
                string _m = Y[1].Replace('0', ' ') + "月";
                string _d = Y[2] + "日";
                return _y + _m.Trim() + _d + "更新";
            }
            return "";
        }
        /// <summary>
        /// 获取周几
        /// </summary>
        /// <returns></returns>
        public static string GetWeek()
        {
            var DayDesc = new[] {
                new { CNDesc = "星期日", DayValue = 0, ENDesc = "Sunday"},
                new { CNDesc = "星期一", DayValue = 1, ENDesc = "Monday"},
                new { CNDesc = "星期二", DayValue = 2, ENDesc = "Tuesday"},
                new { CNDesc = "星期三", DayValue = 3, ENDesc = "Wednesday"},
                new { CNDesc = "星期四", DayValue = 4, ENDesc = "Thursday"},
                new { CNDesc = "星期五", DayValue = 5, ENDesc = "Friday"},
                new { CNDesc = "星期六", DayValue = 6, ENDesc = "Saturday"},
            };
            return DayDesc.Where(d => d.DayValue == (int)DateTime.Now.DayOfWeek).Select(d => d.CNDesc).FirstOrDefault();
        }
        /// <summary>
        /// 歌曲时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static string GetTime(string timeStamp)
        {
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan ts = new TimeSpan(lTime);
            return ts.ToString().Substring(3);
        }

        public static DateTime GetDateToString(string time)
        {
            time = time.Substring(0, 10);
            double timestamp = Convert.ToInt64(time);
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
            return dateTime;
        }
    }
}
